
package com.zzvcom.sms.utils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringUtils;

public class Md5Util {
    private Md5Util() {
    }

    public static MessageDigest getDigest(String algorithm) {
        try {
            return MessageDigest.getInstance(algorithm);
        } catch (NoSuchAlgorithmException var2) {
            throw new IllegalArgumentException(var2);
        }
    }

    public static MessageDigest getMd5Digest() {
        return getDigest("MD5");
    }

    public static byte[] md5(byte[] data) {
        return getMd5Digest().digest(data);
    }

    public static byte[] md5(String data) {
        if (StringUtils.isEmpty(data)) {
            data = "";
        }

        return md5(data.getBytes(StandardCharsets.UTF_8));
    }

    public static String md5Hex(String data) {
        return Hex.encodeHexString(md5(data));
    }
}
